#include <iostream>
#include <unordered_set>

using std::cout;
using std::unordered_set;

class Solution
{
public:
    bool isHappy(int n)
    {
        unordered_set<int> sums;
        int sum = n;
        while (1)
        {
            sum = getSum(sum);
            if (sum == 1)
                return true;
            else if (sums.count(sum) != 0)
                return false;
            else if (sums.count(sum) == 0)
                sums.insert(sum);
            
        }
        return false;
    }

private:
    int getSum(int n)
    {
        int sum = 0;
        while (n != 0)
        {
            sum += ((n % 10) * (n % 10));
            n = n / 10;
        }
        return sum;
    }
};

int main(int argc, char const *argv[])
{
    Solution s;
    cout << s.isHappy(199) << "\n";
    getchar();
    return 0;
}
